package com.streamax.app.cqbus.platform.ext

import com.kt.coroutinedemo.utils.DateTime
import com.swallowsonny.convertextlibrary.toHexString
import java.nio.ByteBuffer
import kotlin.experimental.and


/**
 * 扩展Int
 * int类型转为 2个长度的Byte
 */
val Int.toByteArray: ByteArray
    get() = run {
        byteArrayOf(this.toByte())
    }

/**
 * ByteArray转为Int 和Int.toByteArray配合使用
 */
val ByteArray.toInt: Int
    get() = run {
        if (this.size != 1) {
            0
        } else {
            this[0].toInt() and 0xff
        }
    }

/**
 * 扩展Int
 * int类型转为 2个长度的Byte  高位前低位后
 */
val Int.toByteArray2: ByteArray
    get() = run {
        val byteArray = ByteArray(2)
        var LowH = ((this shr 8) and 0xff).toByte()
        var LowL = (this and 0xff).toByte()
        byteArray[0] = LowH
        byteArray[1] = LowL
        byteArray
    }
val Long.toByteArray2: ByteArray
    get() = run {
        val byteArray = ByteArray(2)
        var LowH = ((this shr 8) and 0xff).toByte()
        var LowL = (this and 0xff).toByte()
        byteArray[0] = LowH
        byteArray[1] = LowL
        byteArray
    }


/**
 * ByteArray转为Int 和Int.toByteArray2配合使用
 */
val ByteArray.toInt2: Int
    get() = run {
        if (this.size != 2) {
            0
        } else {
            val n1 = (this[0].toInt() and 0xff) shl 8
            val n2 = this[1].toInt() and 0xff
            n1 or n2
        }

    }

/**
 * 相加byteArray
 */
val ByteArray.toInt2Add: Int
    get() = run {
        if (this.size != 2) {
            0
        } else {
            val n1 = this[0].toInt() * 100
            val n2 = this[1].toInt()
            n1 + n2
        }

    }


/**
 * 扩展Int
 * int类型转为 4个长度的Byte 高位前低位后
 */
val Int.toByteArray4: ByteArray
    get() = run {
        var byteArray = ByteArray(4)
        var highH = ((this shr 24) and 0xff).toByte()
        var highL = ((this shr 16) and 0xff).toByte()
        var LowH = ((this shr 8) and 0xff).toByte()
        var LowL = (this and 0xff).toByte()
        byteArray[0] = highH
        byteArray[1] = highL
        byteArray[2] = LowH
        byteArray[3] = LowL
        byteArray
    }

/**
 * ByteArray转为Int 和Int.toByteArray4配合使用
 */
val ByteArray.toInt4: Int
    get() = run {
        if (this.size != 4) {
            0
        } else {
            val n1 = this[0].toInt() and 0xff shl 24
            val n2 = this[1].toInt() and 0xff shl 16
            val n3 = this[2].toInt() and 0xff shl 8
            val n4 = this[3].toInt() and 0xff
            n1 or n2 or n3 or n4
        }
    }


/**
 * 将ByteArray转换为DateTime
 */
val ByteArray.toDateTime: DateTime?
    get() = run {
        if (this.size != 6) {
            null
        } else {
            val dateTime = DateTime(this)
            dateTime
        }
    }


/**
 * Int 转BCD 长度2
 */
val Int.toBcdWord2: ByteArray
    get() = run {
        var byteArray = ByteArray(2)
        var temp = this.toString()
        if (temp.length < 4) {
            temp = temp.padStart(4, '0')
        }
        if (temp.length > 4) {
            temp = temp.substring(0, 4)
        }
        byteArray[0] = temp.substring(0, 2).toInt(16).toByte()
        byteArray[1] = temp.substring(2, 4).toInt(16).toByte()
        byteArray
    }


/**
 * Int转BCD
 */
val Int.toBcdWord5: ByteArray
    get() = run {
        var byteArray = ByteArray(5)
        var temp = this.toString()
        if (temp.length < 10) {
            temp = temp.padStart(10, '0')
        }
        if (temp.length > 10) {
            temp = temp.substring(0, 10)
        }
        byteArray[0] = temp.substring(0, 2).toInt(16).toByte()
        byteArray[1] = temp.substring(2, 4).toInt(16).toByte()
        byteArray[2] = temp.substring(4, 6).toInt(16).toByte()
        byteArray[3] = temp.substring(6, 8).toInt(16).toByte()
        byteArray[4] = temp.substring(8, 10).toInt(16).toByte()

        byteArray
    }


val ByteArray.toBcdWordInt6: String
    get() = run {
        val numList = mutableListOf<String>()
        if (this.size == 6) {
//            numList.add(0, (this[0] and 0xff.toByte()).toInt().toString(16))
//            numList.add(1, (this[1].toInt() and 0xff).toString(16))
//            numList.add(2, (this[2].toInt() and 0xff).toString(16))
//            numList.add(3, (this[3].toInt() and 0xff).toString(16))
//            numList.add(4, (this[4].toInt() and 0xff).toString(16))
//            numList.add(5, (this[5].toInt() and 0xff).toString(16))
          val str =   this.toHexString(false)
            val s = 0
        }
//        numList[0] + numList[1] + numList[2] + numList[3] + numList[4] + numList[5]
        this.toHexString(false)
    }

/**
 * 扩展Int
 * int类型转为 8个长度的ByteArray 高位前低位后
 */
val Long.toByteArray8: ByteArray
    get() = run {
        val byteArray = ByteArray(8)
        val b7 = ((this shr 56) and 0xff).toByte()
        val b6 = ((this shr 48) and 0xff).toByte()
        val b5 = ((this shr 40) and 0xff).toByte()
        val b4 = ((this shr 32) and 0xff).toByte()
        val b3 = ((this shr 24) and 0xff).toByte()
        val b2 = ((this shr 16) and 0xff).toByte()
        val b1 = ((this shr 8) and 0xff).toByte()
        val b0 = (this and 0xff).toByte()

        byteArray[0] = b7
        byteArray[1] = b6
        byteArray[2] = b5
        byteArray[3] = b4
        byteArray[4] = b3
        byteArray[5] = b2
        byteArray[6] = b1
        byteArray[7] = b0

        byteArray
    }

val Long.toByteArray9: ByteArray
get() = run {
    val byteArray = ByteArray(9)
    val b8 = ((this shr 72) and 0xff).toByte()
    val b7 = ((this shr 56) and 0xff).toByte()
    val b6 = ((this shr 48) and 0xff).toByte()
    val b5 = ((this shr 40) and 0xff).toByte()
    val b4 = ((this shr 32) and 0xff).toByte()
    val b3 = ((this shr 24) and 0xff).toByte()
    val b2 = ((this shr 16) and 0xff).toByte()
    val b1 = ((this shr 8) and 0xff).toByte()
    val b0 = (this and 0xff).toByte()

    byteArray[0] = b8
    byteArray[1] = b7
    byteArray[2] = b6
    byteArray[3] = b5
    byteArray[4] = b4
    byteArray[5] = b3
    byteArray[6] = b2
    byteArray[7] = b1
    byteArray[8] = b0

    byteArray
}
/**
 * ByteArray转为Int 和Int.toByteArray8配合使用
 */
val ByteArray.toLong8: Long
    get() = run {
        val buffer = ByteBuffer.allocate(8)
        buffer.put(this, 0, this.size)
        buffer.flip() //need flip
        buffer.getLong()
    }

